package school.valon.rest.services;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import school.valon.business.ValidationRulesBusiness;
import school.valon.dto.ValidationRulesDto;

@Path("/validationRules")
@PermitAll
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public class ValidationRulesService {
	@Autowired()
	@Qualifier("validationRulesBusiness")
	private ValidationRulesBusiness validationRulesBusiness;

	@POST
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createSurveyQuestion(ValidationRulesDto validationRulesDto) throws Exception {
		validationRulesDto = validationRulesBusiness.saveValidationRules(validationRulesDto);
		return Response.status(Response.Status.CREATED).entity(validationRulesDto).build();
	}

	@PUT
	@Path("{validationRulesId}")
	@RolesAllowed("staff")
	public Response updateSurveyQuestion(@PathParam("validationRulesId") Integer validationRulesId,
			ValidationRulesDto validationRulesDto) throws Exception {
		validationRulesDto.setValidationRulesId(validationRulesId);
		validationRulesDto = validationRulesBusiness.saveValidationRules(validationRulesDto);
		return Response.ok("Validation rules updated").build();
	}

	@DELETE
	@Path("{validationRulesId}")
	@RolesAllowed("staff")
	public Response deleteSurveyQuestion(@PathParam("validationRulesId") Integer validationRulesId) throws Exception {
		validationRulesBusiness.deleteValidationRules(validationRulesId);
		return Response.ok("Validation rules deleted").build();
	}
}
